//
//  MainViewController.swift
//  TakeTime
//
//  Created by bachelor on 16/10/22.
//  Copyright © 2016年 bachelor. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setValue(MainTabBar(), forKey: "tabBar")
        
        let tab = self.tabBar as! MainTabBar
        tab.composeButton.addTarget(self, action: #selector(MainViewController.composeBtnClick), for: .touchUpInside)
        self.addChildViewControllers()
        
    }
    
    /// 低栏按钮点击事件
    func composeBtnClick() {
        print(#function)
    }
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orange
        
        
        addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
        addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
        addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
        addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        
    
    }
    /// 动态添加子控制器
    private func addChildViewController(_ vcName: String, title: String, imageName: String) {
        
        let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        let clsName = namespace + "." + vcName
        let cls: AnyClass = NSClassFromString(clsName)!
        
        
        /// 解析AnyClass
        guard let vcType = cls as? UIViewController.Type else {
            return
        }
        let vc = vcType.init()
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.title = title
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
